package erfgame.frontend.desktop.view.terrain.iso.interaction;

import erfgame.core.Point3D;
import erfgame.core.Rotation;
import erfgame.core.entity.Entity;

import erfgame.frontend.desktop.view.terrain.iso.IsometricTerrainView;
import erfgame.frontend.desktop.view.terrain.iso.handler.InteractionHandler;

public class ViewRotationInteractionFactory implements EntityInteractionFactory {
	
	private IsometricTerrainView view;
	private int delta;
	
	public ViewRotationInteractionFactory( IsometricTerrainView view, int delta ) {
		this.view = view;
		this.delta = delta;
	}

	public Interaction createInteraction(Entity entity, Point3D terrain) {
		return new ViewRotationInteraction( terrain );
	}
	
	protected Rotation getTargetRotation( Rotation currentRotation ) {
		Rotation[] rotations = Rotation.values();
		int ordinalValue = currentRotation.ordinal() + delta;
		if( ordinalValue >= rotations.length ) {
			ordinalValue -= rotations.length;
		} else if( ordinalValue < 0 ) {
			ordinalValue += rotations.length;
		}
		return rotations[ ordinalValue ];
	}

	private class ViewRotationInteraction implements Interaction {

		private Point3D around;
		
		public ViewRotationInteraction( Point3D around ) {
			this.around = around;
		}
		
		public InteractionHandler activate() {
			Rotation rotation = getTargetRotation( view.getCurrentRotation() );
			view.setCurrentRotation( rotation, around );
			return null;
		}
	}
}
